package com.feihao.zhou.servlet;

import com.feihao.zhou.domain.Student;
import com.feihao.zhou.service.StudentService;
import com.mysql.fabric.xmlrpc.base.Data;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/updateStudentServlet")
public class UpdateStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*Map<String, String[]> parameterMap = req.getParameterMap();
        Student student = new Student();

        try {
            BeanUtils.populate(student,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }*/

        Student student = null;
        try {
            Integer sid = Integer.valueOf(req.getParameter("sid"));
            String name = req.getParameter("name");
            Integer age = Integer.valueOf(req.getParameter("age"));
            String birthday = req.getParameter("birthday");
            Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
            student = new Student(sid, name, age, parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        //调用service的UpdateStudnet（）：
        StudentService studentService = new StudentService();
        studentService.updateStudent(student);

        //重定向到studentServlet
        resp.sendRedirect("/studentServlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
